package com.vx.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.commons.math3.distribution.EnumeratedIntegerDistribution;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.vx.ratingTool.Player;
import com.vx.ratingTool.bo.PlayerBOimpl;
import com.vx.ratingTool.dao.PlayerDAOimpl;

@Controller
@RequestMapping("/createplayers")
public class CreatePlayersController {

	private PlayerDAOimpl playerDAO;
	private PlayerBOimpl playerBO;

	public PlayerDAOimpl getPlayerDAOimpl() {
		return playerDAO;
	}

	@Autowired
	public void setPlayerDAOimpl(PlayerDAOimpl playerDAO) {
		this.playerDAO = playerDAO;
	}

	public PlayerBOimpl getPlayerBO() {
		return playerBO;
	}

	@Autowired
	public void setPlayerBO(PlayerBOimpl playerBO) {
		this.playerBO = playerBO;
	}

	// private SessionFactory sessionFactory;
	private List<Player> players;

	@RequestMapping(method = RequestMethod.GET)
	public String CreatePlayers(Model model) {
		players = new ArrayList<Player>();

		// generating distributed values
		int numSamples = 1000;

		int[] numsToGenerate = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
		double[] discreteProbabilities = new double[] { 0.02, 0.05, 0.09, 0.14, 0.2, 0.2, 0.14, 0.09, 0.05, 0.02 };

		EnumeratedIntegerDistribution distribution = new EnumeratedIntegerDistribution(numsToGenerate, discreteProbabilities);

		// creating new players with distributed skill
		int[] samples = distribution.sample(numSamples);
		Random rnd = new Random();
		for (int i = 0; i < numSamples; i++) {
			int skill = (int) ((samples[i] - 1) * 10 + (rnd.nextDouble() * 10));
			String skilltag = "Normal";
			if (skill > 95) {
				skilltag = "VeryHigh";
			} else {
				if (skill > 80) {
					skilltag = "High";
				}
			}
			Player aPlayer = new Player((i + 1), "(" + skilltag + ")Bot #" + (i + 1), skill, 1500, 5, 0);
			players.add(aPlayer);
		}

		playerBO.clearDatabase();
		playerBO.saveList(players);

		model.addAttribute("players", playerBO.GetListOfPlayers());

		return "createplayers";
	}

}
